/**
 * Created by JasonChou on 16/5/2.
 */
var debug = false;

var agent = {
    navigator: navigator.userAgent,
    match: function (Reg) {
        return this.navigator.match(Reg);
    },
    isWechat: navigator.userAgent.match(/MicroMessenger/g) != null,
    isClient: navigator.userAgent.match(/Tattoo/g) != null,
    isAndroid: navigator.userAgent.match(/Android/gi) != null,
    isIos: navigator.userAgent.match(/ios/gi) != null
};

var db = {
    appid: 'wxe5e08d0563591e11',
    w: window.screen.width,
    h: window.screen.height,
    wx_code: getQuery('code'),  //如果存在的话，是授权页过来
    wx_openId: getQuery('open_id') //如果存在的话，是其他相关页面过来
};

function getQuery(key) {
    var res = location.href.match(new RegExp(key + '=([^&#]+)'));
    return res ? res[1] : null;
}

if (!db.wx_openId && agent.isWechat && !db.wx_code) {
    var url = location.href;

    var redirectUrl = encodeURIComponent(url);
    var authUrl = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + db.appid
        + '&redirect_uri=' + redirectUrl
            //+ '&response_type=code&scope=snsapi_base'
        + '&response_type=code&scope=snsapi_userinfo'
        + '#wechat_redirect';

    location = authUrl;
}

var api = 'http://api.qicai.wenshendaka.com/';
//var api='http://127.0.0.1:3003/';
//var api = 'http://192.168.199.122:3003/';
var positionCache = {};


db.data = {};

var refreshCache={};

db.set = function (key, value) {
    if (typeof key == 'string') {
        this.data[key] = value;
    }
};

db.get = function (key) {
    if (typeof key == 'string') {
        return this.data[key] || null;
    }
};

db.clear = function (key) {
    if (typeof key == 'string') {
        var tmp = this.data[key] = null;
    }
};
